package com.wonders.auth.module.sys.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wonders.auth.module.fo.UserFo;
import com.wonders.auth.module.po.User;
import com.wonders.auth.module.sys.service.IUserService;
import com.wonders.auth.module.vo.BaseVo;
import com.wonders.auth.module.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @author wonders
 */
@Controller
@RequestMapping("/sys/user")
public class UserController {
    @Autowired
    private IUserService userService;

    @RequestMapping("/page/{page}")
    public String page(@PathVariable("page") String page) {
        return "page/user/" + page;
    }

    @RequestMapping("/list")
    @ResponseBody
    public BaseVo list(UserFo userFo) {
        IPage<User> page = new Page<>(userFo.getPage(), userFo.getLimit());
        IPage<UserVo> list = userService.selectPageVo(page, userFo);
        BaseVo<List<UserVo>> baseVo = new BaseVo();
        baseVo.setCount((int) list.getTotal());
        baseVo.setData((List<UserVo>) list.getRecords());
        return baseVo;
    }

    @RequestMapping("/exist")
    @ResponseBody
    public BaseVo<Boolean> exist(String name) {
        BaseVo<Boolean> baseVo = new BaseVo();
        Boolean data;
        int count = userService.count(new QueryWrapper<User>().eq("username", name));
        if (count > 0) {
            data = true;
        } else {
            data = false;
        }
        baseVo.setData(data);
        return baseVo;
    }
}
